package com.zhiyou100.controller;

import com.zhiyou100.annotation.Auth;
import com.zhiyou100.model.User;
import com.zhiyou100.serice.UserSerice;
import com.zhiyou100.util.ResultObject;
import com.zhiyou100.util.SendSmsUtil;
import com.zhiyou100.util.UtilS;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.HttpSession;

@RequestMapping("/user")
@Controller
public class UserController {

    @Autowired
    private UserSerice serice;
    @Autowired
    private JedisPool jedisPool;

    @RequestMapping(value =  "/login" ,method = RequestMethod.POST)
    @ResponseBody@Auth("这是一个前台登陆的操作")
    /**
     * 这是登录请求,是前台的ajax请求
     */
    public ResultObject login(User user, HttpSession session){
        System.out.println("前台的数据 : "+user);
        //获得subject
        Subject subject = SecurityUtils.getSubject();
        try{
            //用shiro进行登录,进入自定义的MyReaml类,如果有异常说明MyReaml类中验证用户登录失败
            subject.login(new UsernamePasswordToken(user.getEmail(),user.getPassword()));
            //从shiro框架中得到登录的user对象
            user = (User) subject.getPrincipal();
            //存入session中
            session.setAttribute("user",user);
            System.out.println("登录成功 user 为 : " +user);
            return new ResultObject("200","账号密码正确登录成功",user);
        }catch (AuthenticationException e){
            System.out.println("邮箱未注册");
            return new ResultObject("404","账号或密码错误登录失败",null);
        }
    }
    @RequestMapping(value = "/right",method = RequestMethod.POST)
    @ResponseBody@Auth("这是一个注册的操作")
    public ResultObject right(User user,String psw_again,String teml , String key){
        Jedis resource = jedisPool.getResource();
        boolean judge = serice.right(user,psw_again);
        System.out.println(judge);
        System.out.println(teml);
        System.out.println(key);
        System.out.println(resource.get(key));
        if(judge&&teml!=null&&resource.strlen(key)!=0 && resource.get(key).equals(teml)){
            User userByEmail = serice.findUserByEmail(user.getEmail());
            return new ResultObject("200","注册成功",userByEmail);
        }
        return new ResultObject("404","手机号或邮箱已注册",judge);
    }
    @RequestMapping(value = "/validateEmail",method = RequestMethod.POST)
    @ResponseBody@Auth("这是通过邮箱查用户的操作的操作")
    public  String validateEmail(String email){
        User user = serice.findUserByEmail(email);
        if (user == null || user.getId()==null ||user.getId() ==0){
            return "success";
        }
        return "";
    }
    @RequestMapping(value = "/behindLogin",method = RequestMethod.POST)@Auth("这是一个后台登陆的操作")
    public String behindLogin(String username , String password, HttpSession session, Model model){
        User user = (User) session.getAttribute("user");
        password = new Md5Hash(password, user.getCalt(), 10).toString();

        if (user != null && username !=null && password != null && user.getPhoneNum().equals(username)&&user.getPassword().equals(password)){
            return "behind/video/listVideo";
        }
        model.addAttribute("msg","手机号或密码错误");
        return "forward:/behind/login.jsp";
    }
    @RequestMapping(value =  "/logout")@Auth("这是一个退出登录的操作")
    public String logout(){
        //获得subject
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
//        Object user = session.getAttribute("user");
//        System.out.println(user);
        return "forward:/index.jsp";
    }
    @RequestMapping(value =  "/{id}",method = RequestMethod.GET)
    @ResponseBody@Auth("这是一个通过id查用户的操作")
    public ResultObject findById(@PathVariable("id") Integer id){
        //获得subject
        User user = serice.findById(id);
        return new ResultObject("200","个人信息",user);
    }
    int i = 100000000;
    @RequestMapping(value =  "/teml/{id}",method = RequestMethod.POST)
    @ResponseBody@Auth("这是一个发送短信的操作")
    public ResultObject teml(@PathVariable("id") String phone){
        Jedis jedis = jedisPool.getResource();
        String teml = UtilS.getTeml();
        jedis.set("teml"+i,teml);
        jedis.expire("teml"+ i,300);
        SendSmsUtil.SendSms(phone,teml);

        return new ResultObject("200","个人信息","teml"+i--);
    }


}
